local string_format = string.format 
local table_concat = table.concat
local math_abs = math.abs 

local affix = {}

---词缀表
affix.list = {

	{'生命', '|cff8ef48e生命上限+%.0f|r', '|cffff6565生命上限-%.0f|r',},
	{'魔法', '|cff8ef48e魔法上限+%.0f|r', '|cffff6565魔法上限-%.0f|r',},
	{'额外攻击', '|cff8ef48e攻击力+%.0f|r', '|cffff6565攻击力-%.0f|r',},
	{'额外护甲', '|cff8ef48e护甲+%.0f|r', '|cffff6565护甲-%.0f|r',},
	{'基础攻击', '|cff8ef48e攻击力+%.0f|r', '|cffff6565攻击力-%.0f|r',},
	{'基础护甲', '|cff8ef48e护甲+%.0f|r', '|cffff6565护甲-%.0f|r',},

	{'生命加成', '|cff8ef48e提高%.0f%%生命|r', '|cffff6565降低%.0f%%生命|r',},
	{'魔法加成', '|cff8ef48e提高%.0f%%魔法|r', '|cffff6565降低%.0f%%魔法|r',},
	{'攻击加成', '|cff8ef48e提高%.0f%%攻击|r', '|cffff6565降低%.0f%%攻击|r',},
	{'护甲加成', '|cff8ef48e提高%.0f%%护甲|r', '|cffff6565降低%.0f%%护甲|r',},
	
	{'生命恢复', '|cff8ef48e生命恢复+%.02f|r', '|cffff6565每秒损失%.f点生命|r',},
	{'魔法恢复', '|cff8ef48e魔法恢复+%.02f|r', '|cffff6565每秒损失%.f点魔法|r',},

	{'攻击间隔', '|cffff6565攻击间隔+%.02f|r', '|cff8ef48e攻击间隔-%.02f|r',},
	{'攻击速度', '|cff8ef48e攻击速度+%.0f%%|r', '|cffff6565攻击速度-%.0f%%|r',},
	{'攻击距离', '|cff8ef48e攻击距离提高%.0f码|r', '|cffff6565攻击距离降低%.0f码|r',},
	
	{'治疗效果', '|cff8ef48e提高%.01f%%治疗效果|r'},
	{'冷却缩减', '|cff8ef48e冷却缩减+%.0f%%|r', '|cffff6565冷却缩减-%.0f%%|r'},

	{'移动速度', '|cff8ef48e移动速度+%.0f|r', '|cffff6565移动速度-%.0f|r',},
	{'移动速度%', '|cff8ef48e移动速度+%.0f%%|r', '|cffff6565移动速度-%.0f%%|r',},

	{'护甲穿透', '|cff8ef48e护甲穿透+%.0f|r', '|cffff6565护甲穿透-%.0f|r',},
	{'护甲穿透%', '|cff8ef48e无视护甲+%.0f%%|r', '|cffff6565无视护甲-%.0f%%|r',},

	{'攻击伤害', '|cff8ef48e攻击伤害+%.0f%%|r', '|cffff656减少%.0f%%5攻击伤害|r',},
	{'技能伤害', '|cff8ef48e技能伤害+%.0f%%|r', '|cffff656减少%.0f%%5技能伤害|r',},
	{'攻击减伤', '|cff8ef48e降低受到的%.0f%%攻击伤害|r', '|cffff6565额外承受%.0f%%攻击伤害|r',},
	{'技能减伤', '|cff8ef48e降低受到的%.0f%%技能伤害|r', '|cffff6565额外承受%.0f%%技能伤害|r',},

	{'伤害增幅', '|cff8ef48e伤害增幅+%.0f%%|r', '|cffff656降低%.0f%%5伤害|r',},
	{'伤害减免', '|cff8ef48e减少%.0f%%承受伤害|r', '|cffff6565额外承受%.0f%%伤害|r',},

	{'吸血', '|cff8ef48e吸血+%.0f%%|r'},
	{'控制减免', '|cff8ef48e减少受到的%.0f%%的控制时间|r'},

	{'动画伤害点', '|cffff6565攻击前摇提高%.02f秒|r', '|cff8ef48e攻击前摇降低%.02f秒|r',},
	{'动画恢复点', '|cffff6565攻击后摇提高%.02f秒|r', '|cff8ef48e攻击后摇降低%.02f秒|r',},

	{'额外视野', '|cff8ef48e提高%.0f码视野|r', '|cffff6565降低%.0f码视野|r',},

	{'异常伤害', '|cff8ef48e异常伤害+%.0f%%|r', '|cffff656降低%.0f%%5异常伤害|r',},
}

----词缀类型
affix.types = {}
for index, data in ipairs(affix.list) do
	local name = data[1]
	affix.types[name] = index
end

---- 格式化表
ac.affix = setmetatable({ },{
    __index = affix,
    __call = function(self, object)
        local str = affix.states2string(object)
        if object.state then 
            str = affix.states2string(object.state) .. str
        end 
        return str
    end,
}) 

----添加词缀列表
function affix.add_list(type_name, add_meta, sub_meta)
	if not affix.types[type_name] then 
		local index = #affix.list+1
		affix.types[type_name] = index
		affix.list[index] = {
			type_name, add_meta, sub_meta
		}
	end 
end

---- 根数名称和数值生成词缀描述
function affix.state2string(type_name, value)
	local str = ""
	if type_name and value then 
		local id = affix.types[type_name]
		if id then 
			local meta = affix.list[id]
			if value > 0 and meta[2] then 
				str = string_format(meta[2], value)
			elseif value < 0 and meta[3] then 
				str = string_format(meta[3], math_abs(value))
			end 
		end
	end
	return str
end 

--- 根据一个属性表生成词缀描述
--- @info 表
function affix.states2string(info)
	local adds = {}
	local subs = {}
	local bool = false
	for i=1,#affix.list do 
		local meta = affix.list[i]
		local name = meta[1]
		local data = info[name]
		if data and type(data) == "number" then 
			if data > 0 and meta[2] then 
				adds[#adds+1] = string_format(meta[2], data)
			elseif data < 0 and meta[3] then 
				subs[#subs+1] = string_format(meta[3], math_abs(data))
			end
			bool = true
		end 
	end 
	if bool then 
		local buff = adds 
		for i=1,#subs do 
			buff[#buff+1] = subs[i]
		end 
		buff[#buff+1] = "\r\n"
		-- table.insert(buff, 1, '|cffffcc99属性：|r')
		return table_concat(buff, '\r\n')
	end
	return ""
end 

return affix